/**
 * 音乐播放功能
 */
// 咆哮音乐播放插件
// import { exists } from 'fs';
class Player {
  // 初始化音乐播放器
  play(_this, item) {
    //是否已经在播放
    if (_this.$store.getters.getPlayer.playLoading) {
      _this.$store.commit("setPlayer", ["playLoading", false]);
      _this.$store.getters.getPlayer.playerObj.load();
    }
    let that = this;
    if (_this.$store.getters.getPlayer.playLrcArr !== false) {
      // 清除歌词数据
      _this.$store.commit("setPlayer", ["playLrcArr", false]);
      _this.$store.commit("setPlayer", ["playTimeLine", 0]);
      _this.$store.commit("setPlayer", ["playAllTime", 0]);
    }

    //
    var audioObj = new Audio();
    audioObj.src = item.url;
    audioObj.volum = 0.5;
    audioObj.play();
    let intervalAudio = setInterval(function() {
      if (audioObj.duration > 0) {
        _this.$store.commit("setPlayer", ["play", 1]);
        //存储播放地址ID
        _this.$store.commit("setPlayer", ["playId", item.id]);
        //存储播放状态
        _this.$store.commit("setPlayer", ["playLoading", true]);
        //存储歌曲总时长
        _this.$store.commit("setPlayer", ["playAllTime", audioObj.duration]);
        //存储播放器对象
        _this.$store.commit("setPlayer", ["playerObj", audioObj]);
        //添加到播放列表中去
        that.addPlayerList(_this, [item]);
        //隔一秒获取一次当前播放时间到状态中
        var interval = setInterval(function() {
          if (
            _this.$store.getters.getPlayer.playAllTime >
            _this.$store.getters.getPlayer.playTimeLine
          ) {
            if (_this.$store.getters.getPlayer.playerObj != false) {
              _this.$store.commit("setPlayer", [
                "playTimeLine",
                audioObj.currentTime
              ]);
            }
          } else {
            console.log("结束播放");
            clearInterval(interval);
            that.playDownSong(_this);
          }
        }, 100);
        clearInterval(intervalAudio);
        console.log("结束播放all");
      }
    }, 100);
  }

  //添加歌曲到播放列表
  addPlayerList(_this, arr) {
    let playList = _this.$store.getters.getPlayer.playList;
    if (playList === false) {
      _this.$store.commit("setPlayer", ["playList", arr]);
    } else {
      for (let j = 0; j < arr.length; j++) {
        let res = this.ifArrVal(playList, arr[j]["id"]);
        if (res == 1) {
          continue;
        } else {
          playList.push(arr[j]);
        }
      }
      _this.$store.commit("setPlayer", playList);
    }
  }

  //多维数组判断是否存在某值
  ifArrVal(arr, value) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i]["id"] == value) {
        return 1; //存在
      }
    }
    return -1; //不存在
  }

  // 获取当前播放器正在播放的歌曲详情
  getSongDetail(_this) {
    if (_this.$store.getters.getPlayer.playLoading === false) {
      return false;
    }
    let playList = _this.$store.getters.getPlayer.playList;
    let playId = _this.$store.getters.getPlayer.playId;
    for (let index = 0; index < playList.length; index++) {
      if (playList[index]["id"] == playId) {
        return playList[index];
      }
    }
  }

  //获取当前播放百分比
  getPlayerPlayTimeLine(_this) {
    let playTimeLine = _this.$store.getters.getPlayer.playTimeLine;
    let playAllTime = _this.$store.getters.getPlayer.playAllTime;
    if (playAllTime === false) {
      return false;
    }
    return ((playTimeLine / playAllTime) * 100).toFixed(0);
  }

  // 播放上一首
  playUpSong(_this) {
    if (_this.$store.getters.getPlayer.playLoading === false) {
      return false;
    }
    if (_this.$store.getters.getPlayer.playModel == 2) {
      this.randPlaySong(_this);
    } else {
      let playList = _this.$store.getters.getPlayer.playList;
      let playId = _this.$store.getters.getPlayer.playId;
      for (let index = 0; index < playList.length; index++) {
        if (playList[index]["id"] == playId) {
          if (index == 0) {
            return false;
          } else {
            this.play(_this, playList[index - 1]);
          }
        }
      }
    }
  }

  // 播放下一首
  playDownSong(_this) {
    if (_this.$store.getters.getPlayer.playLoading === false) {
      return false;
    }
    if (_this.$store.getters.getPlayer.playModel == 2) {
      this.randPlaySong(_this);
    } else {
      let playList = _this.$store.getters.getPlayer.playList;
      let playId = _this.$store.getters.getPlayer.playId;
      for (let index = 0; index < playList.length; index++) {
        if (playList[index]["id"] == playId) {
          if (index + 1 == playList.length) {
            return false;
          } else {
            this.play(_this, playList[index + 1]);
          }
        }
      }
    }
  }

  // 随机播放
  randPlaySong(_this) {
    if (_this.$store.getters.getPlayer.playModel == 1) {
      this.playDownSong(_this);
    } else {
      let playList = _this.$store.getters.getPlayer.playList;
      let playListIndex = playList.length;
      let num = this.randomNum(0, playListIndex);
      this.play(_this, playList[num]);
    }
  }

  //生成从minNum到maxNum的随机数
  randomNum(minNum, maxNum) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * minNum + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
        break;
      default:
        return 0;
        break;
    }
  }
}

export default Player;
